<template>
	<view class="content">
		<view class="header">
			<image class="header-image" :src="headerIamge"></image>
		</view>
		<view class="coupon-content">
			<view class="coupon-steps">
				<view class="coupon-step">
					<view class="coupon-step-icon">
						<image class="coupon-step-icon-img" src="/static/icon/coupon-step.png"></image>
					</view>
					<view class='coupon-step-title'>先领券</view>
				</view>
				<image class="coupon-arrow" src="/static/icon/right-arrow.png"></image>
				<view class="coupon-step">
					<view class="coupon-step-icon">
						<image class="coupon-step-icon-img" src="/static/icon/step-order.png"></image>
					</view>
					<view class='coupon-step-title'>再下单</view>
				</view>
				<image class="coupon-arrow" src="/static/icon/right-arrow.png"></image>
				<view class="coupon-step">
					<view class="coupon-step-icon">
						<image class="coupon-step-icon-img" src="/static/icon/step-fanli.png"></image>
					</view>
					<view class='coupon-step-title'>拿返利</view>
				</view>
			</view>
			<view class="coupon-btn" @click="gotoTbEle">领券点外卖</view>
		</view>
		<view class="share-content">
			<view class="share-mini-box">
				<view class="share-mini-title" @click="sharePosterToWx">分享饿了么小程序</view>
				<view class="share-mini-subtitle">下单更便捷</view>
				<view class='share-mini-notify'>推荐</view>
			</view>
			<view class="copy-tkl-btn" @click="copyTkl">复制饿了么口令</view>
			<view class="share-notify">好友使用您分享的小程序码图/口令下单,您可以享受返现</view>
		</view>
		<view class="rule">
			<view class='rule-item' v-for="rule in rules">{{rule}}</view>
		</view>
		<view class='faraway'>
			<view class="wrapper"><canvas class="wrapper-canvas" canvas-id="canvas"></canvas></view>
		</view>
	</view>
</template>

<script>
	import Common from '@/common/common.js';
	import ClipBoard from '@/js_sdk/dc-clipboard/clipboard.js';
	
	const plug = uni.requireNativePlugin('Html5app-Baichuan');

	export default {
		data() {
			return {
				headerIamge: 'https://oss.pindodo.top/Images/2020/05/25/elemheader.png',
				promotionSceneId: 1571715733668,
				postCover: 'https://oss.pindodo.top/Images/2020/05/25/elem.jpg',
				qrImagePath: '',
				postPath: '',
				destUrl: '',
				tkl: '',
				miniProgramPath: '',
				rules: ['活动说明', '1.省咖用户下单均有佣金，以实际支付金额为准，最高返现4.8%。', '2.下单后最晚半小时内同步订单，请耐心等待。', '3.外卖配送员送货到家后即为确认收货。']
			}
		},
		onLoad: function(option) {
			var self = this;
			if (option.data) {
				self.promotionSceneId = option.data;
			}
			self.getEleShareInfo();
		},
		onHide:function(){
			uni.hideLoading();
		},
		methods: {
			gotoTbEle: function() {
				var self = this;
				if(!self.destUrl) {
					return;
				}
				var tbLinkType = 3;
				Common.gotoTb(plug, self.destUrl, tbLinkType, null);
			},
			copyTkl: function() {
				var self = this;
				if (!self.tkl) {
					return;
				}
				ClipBoard.setText(self.tkl);
				self.$store.commit('setIngoreCopyData', self.tkl);
				uni.showToast({
					icon: 'none',
					title: '口令复制成功 快去分享给好友吧'
				})
			},
			getEleShareInfo: function() {
				var self = this;
				Common.commonRequest({
					url: Common.baseServerUrl + '/Api/TaoBao/PromUrl/GetActivityInfoUrlByRid',
					data: {
						PromotionSceneId: self.promotionSceneId
					},
					success: function(ret) {
						var data = ret.data;
						if (data.Code == 0) {
							self.qrImagePath = data.Data.QrCodeUrl;
							self.miniProgramPath = data.Data.MiniProgramPath;
							self.tkl = data.Data.TaoKouLing;
							self.destUrl = data.Data.DestUrl;
							// 生成海报
							if (self.qrImagePath) {
								// self.createPost();
							}
						}
					},
					fail: function() {
						uni.hideLoading();
					}
				})
			},
			createPost: function(cb) {
				var self = this;
				var ctx = uni.createCanvasContext('canvas');
				var coverImg = self.postCover;
				// 绘制背景海报
				ctx.drawImage(coverImg, 0, 0, uni.upx2px(621), uni.upx2px(1104));
				// 绘制二维码
				ctx.drawImage(self.qrImagePath, uni.upx2px(212), uni.upx2px(740), uni.upx2px(210), uni.upx2px(210));

				ctx.draw(false, () => {
					self.saveTempPoster(cb);
				})
			},
			saveTempPoster: function(cb) {
				var self = this;
				uni.canvasToTempFilePath({
					x: 0,
					y: 0,
					canvasId: 'canvas',
					success: function(res) {
						self.postPath = res.tempFilePath;
						if(cb && typeof(cb)== 'function') {
							cb();
						}
					}
				})
			},
			sharePosterToWx: function() {
				var self = this;
				if(!self.qrImagePath) {
					return;
				}
				uni.showLoading({
					title: '生成海报中...'
				})
				self.createPost(function() {
					uni.share({
						provider: 'weixin',
						type: 2,
						scene: 'WXSceneSession',
						imageUrl: self.postPath,
						success: function() {
							uni.hideLoading();
						}
					})
				});
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 750rpx;
		min-height: 100vh;
		background-color: #ee5f4e;
	}
	
	.header-image {
		width: 750rpx;
		height: 417rpx;
	}

	.coupon-content,
	.share-content {
		width: 710rpx;
		background-color: #FFFFFF;
		border-radius: 15rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 40rpx 0;
		margin-bottom: 20rpx;
	}

	.share-mini-box {
		width: 450rpx;
		height: 90rpx;
		border-radius: 50rpx;
		background-image: linear-gradient(to right, #fc2e5e, #ff040b);
		position: relative;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.share-mini-notify {
		width: 70rpx;
		height: 40rpx;
		border-radius: 50rpx;
		font-size: 20rpx;
		line-height: 40rpx;
		text-align: center;
		color: #000000;
		background-image: linear-gradient(to right, #fcd93c, #f7b91c);
		position: absolute;
		top: -15rpx;
		right: -30rpx;
	}

	.share-mini-title {
		font-size: 32rpx;
		color: #FFFFFF;
		font-weight: bold;
		margin-top: 5rpx;
	}

	.share-mini-subtitle {
		font-size: 21rpx;
		color: #FFFFFF;
	}

	.copy-tkl-btn {
		width: 450rpx;
		height: 85rpx;
		border-radius: 50rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #fe070f;
		border: #fe070f solid 1rpx;
		font-size: 32rpx;
		margin-top: 30rpx;
	}

	.coupon-btn {
		width: 450rpx;
		height: 85rpx;
		border-radius: 50rpx;
		background-image: linear-gradient(to right, #fc2e5e, #ff040b);
		display: flex;
		align-items: center;
		justify-content: center;
		color: #FFFFFF;
		font-size: 30rpx;
		margin-top: 30rpx;
	}

	.share-notify {
		font-size: 25rpx;
		color: rgba(253, 26, 53, .57);
		margin-top: 30rpx;
	}

	.rule {
		width: 680rpx;
		margin-top: 20rpx;
		margin-bottom: 100rpx;
	}

	.rule-item {
		font-size: 28rpx;
		line-height: 42rpx;
		color: #FFFFFF;
	}

	.coupon-steps {
		display: flex;
		align-items: center;
		justify-content: space-around;
		width: 540rpx;
	}

	.coupon-step {
		width: 100rpx;
		display: flex;
		flex-direction: column;
	}

	.coupon-step-icon {
		width: 100rpx;
		height: 100rpx;
		border-radius: 50rpx;
		background-color: #fe1124;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.coupon-step-icon-img {
		width: 55rpx;
		height: 55rpx;
	}

	.coupon-step-title {
		font-size: 25rpx;
		color: #333333;
		text-align: center;
		width: 100rpx;
		margin-top: 10rpx;
	}

	.coupon-arrow {
		width: 80rpx;
		height: 80rpx;
		margin-bottom: 30rpx;
	}

	.faraway {
		position: fixed;
		left: 99999rpx;
		top: 99999rpx;
	}

	.wrapper-canvas {
		width: 621rpx;
		height: 1104rpx;
	}

	.post-image {
		width: 621rpx;
		height: 1104rpx;
	}
</style>
